package com.czumi.javawx.web.controller.weixin;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.czumi.javawx.dao.entity.SysConfig;
import com.czumi.javawx.service.WeixinFanService;
import com.github.sd4324530.fastweixin.api.UserAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.entity.UserInfo;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoListResponse;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.api.response.GetUsersResponse;
import com.github.sd4324530.fastweixin.handle.EventHandle;
import com.github.sd4324530.fastweixin.handle.MessageHandle;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.github.sd4324530.fastweixin.message.req.BaseEvent;
import com.github.sd4324530.fastweixin.message.req.MenuEvent;
import com.github.sd4324530.fastweixin.message.req.TextReqMsg;
import com.github.sd4324530.fastweixin.message.req.VideoReqMsg;
import com.github.sd4324530.fastweixin.message.req.VoiceReqMsg;
import com.github.sd4324530.fastweixin.servlet.WeixinControllerSupport;

@RestController
@RequestMapping("/weixin")
public class WeixinController extends WeixinControllerSupport {

	private static final Logger log = LoggerFactory.getLogger(WeixinController.class);

	protected ApiConfig apiConfig = null;

	protected UserAPI userAPI;

	@Autowired
	protected SysConfig sysConfig;

	@Autowired
	private WeixinFanService weixinFanService;

	@Override
	protected String getToken() {
		// TODO Auto-generated method stub
		apiConfig = new ApiConfig(sysConfig.getAppid(), sysConfig.getSecret(), false);
		userAPI = new UserAPI(apiConfig);
		return sysConfig.getToken();
	}

	// 重写父类方法，处理对应的微信消息
	@Override
	protected BaseMsg handleTextMsg(TextReqMsg msg) {
		String content = msg.getContent();
		log.debug("用户发送到服务器的内容:{}", content);
		UserAPI userAPI = new UserAPI(apiConfig);
		GetUserInfoResponse userinfo = userAPI.getUserInfo("ohqJWuNVHKruqn5KRrDADSrws9AM");
		GetUsersResponse users =userAPI.getUsers("");

		List<UserInfo> list = new ArrayList<>();
		GetUserInfoListResponse tmp = userAPI.getUserInfoList(list);
		return new TextMsg("服务器回复用户消息!");
	}

	@Override
	protected BaseMsg handleVideoMsg(VideoReqMsg msg) {
		// TODO Auto-generated method stub
		return super.handleVideoMsg(msg);
	}

	@Override
	protected BaseMsg handleVoiceMsg(VoiceReqMsg msg) {
		// TODO Auto-generated method stub
		return super.handleVoiceMsg(msg);
	}

	@Override
	protected BaseMsg handleMenuClickEvent(MenuEvent event) {
		// TODO Auto-generated method stub

		return super.handleMenuClickEvent(event);
	}

	@Override
	protected BaseMsg handleMenuViewEvent(MenuEvent event) {
		// TODO Auto-generated method stub
		return super.handleMenuViewEvent(event);
	}

	@Override
	protected BaseMsg handleSubscribe(BaseEvent event) {
		// TODO Auto-generated method stub
		System.out.println("关注");
		GetUserInfoResponse userinfo = userAPI.getUserInfo(event.getFromUserName());
		weixinFanService.saveAndUpdate(userinfo);
		return super.handleSubscribe(event);
	}

	@Override
	protected BaseMsg handleUnsubscribe(BaseEvent event) {
		// TODO Auto-generated method stub
		System.out.println("取消关注");
		GetUserInfoResponse userinfo = userAPI.getUserInfo(event.getFromUserName());
		weixinFanService.saveAndUpdate(userinfo);
		return super.handleUnsubscribe(event);
	}

	/*
	 * 1.1版本新增，重写父类方法，加入自定义微信消息处理器 不是必须的，上面的方法是统一处理所有的文本消息，如果业务觉复杂，上面的会显得比较乱
	 * 这个机制就是为了应对这种情况，每个MessageHandle就是一个业务，只处理指定的那部分消息
	 */
	@Override
	protected List<MessageHandle> initMessageHandles() {
		List<MessageHandle> handles = new ArrayList<MessageHandle>();
		handles.add(new MyMessageHandle());
		return handles;
	}

	// 1.1版本新增，重写父类方法，加入自定义微信事件处理器，同上
	@Override
	protected List<EventHandle> initEventHandles() {
		List<EventHandle> handles = new ArrayList<EventHandle>();
		handles.add(new MyEventHandle());
		return handles;
	}

}
